package cn.jdy.plugins.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.HttpUtil;
import cn.jdy.framework.util.JsonUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.entity.News;
import cn.jdy.platform.service.ConfService;
import cn.jdy.platform.service.NewsService;
import cn.jdy.plugins.entity.Member;

/**
 * @author wuzhiliang
 * @title 新闻控制类
 * @date 2014-03-12
 */
@Controller
@RequestMapping("/news")
public class NewsCtrl extends BaseCtrl {
	@Autowired
	private NewsService newsService;
	@Autowired
	private ConfService confService;
	
	/**
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/{typeid}.html")
	public ModelAndView query(News news, @PathVariable Long typeid) {
		ModelAndView mv = new ModelAndView("/news/newsList.jsp");
		try {
			//襄阳市公安局，需要加入当前时间段周数
			Date datetime = new Date();
			int weekdays = datetime.getDay();
			mv.addObject("weekdays",weekdays);
			
			// 设置分页信息
			if (news.getiDisplayLength() == 0)
				news.setiDisplayLength(10); // 默认长度24
			if (news.getPageNo() <= 0)
				news.setPageNo(1); // 默认页数1
			news.setiDisplayStart((news.getPageNo() - 1)
					* news.getiDisplayLength()); // 显示起始索引
			news.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			if (news.getKeyword() != null && !news.getKeyword().trim().isEmpty()) {
				mv.addObject("dataList",newsService.queryByLike(news, news.getKeyword()));
				mv.addObject("total", newsService.queryByLikeCount(news, news.getKeyword()));
			} else {
				news.setColumnId(typeid);// 新闻分类
				mv.addObject("dataList", newsService.query(news));
				mv.addObject("total", newsService.count(news));
			}
			mv.addObject("pageNo", news.getPageNo());
			mv.addObject("size", news.getiDisplayLength());
			ConfValue cv = confService.find(typeid); // 获取分类的父id,查询同级分类
			mv.addObject("typeName", cv.getValue0());// 分类的名称 用于 页面导航路径展示
			news.setCv(cv);
			if (0 == cv.getPid()) {
				mv.addObject("newsClass", null);// 根据大分类 查询小分类 列表
			} else {
				List<ConfValue> plist = confService.queryByType(
						GlobalConstant.TYPE_NEWS, cv.getPid());// 新闻分类
				mv.addObject("newsClass", plist);// 根据大分类 查询小分类 列表
				// ConfValue pcv = confService.find(cv.getPid()); // 查询父分类名称
				// mv.addObject("typeName", pcv.getValue0());// 父分类的名称 用于
				// 页面导航路径展示
			}
			mv.addObject("news", news);
			mv.addObject("typeid", typeid);
			mv.addObject("key", news.getKeyword()); 
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}

	/**
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/queryimg.html")
	public ModelAndView querybyimg(News news) {
		ModelAndView mv = new ModelAndView("/news/newsImgList.jsp");
		try {
			//襄阳市公安局，需要加入当前时间段周数
			Date datetime = new Date();
			int weekdays = datetime.getDay();
			mv.addObject("weekdays",weekdays);
			
			// 设置分页信息
			if (news.getiDisplayLength() == 0)
				news.setiDisplayLength(10); // 默认长度24
			if (news.getPageNo() <= 0)
				news.setPageNo(1); // 默认页数1
			news.setiDisplayStart((news.getPageNo() - 1)
					* news.getiDisplayLength()); // 显示起始索引
			news.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			
				
				mv.addObject("dataList", newsService.querybyimg(news));
				mv.addObject("total", newsService.imgcount(news));
			
			mv.addObject("pageNo", news.getPageNo());
			mv.addObject("size", news.getiDisplayLength());
			
			
			
			mv.addObject("news", news);
			
			mv.addObject("key", news.getKeyword()); 
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}
	
	/**
	 * 新闻详细内容页
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView queryDetail(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/news/newsDetail.jsp");
		
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		
		try {
			News news = newsService.find(id);
			if (0 == news.getCv().getPid()) {
				mv.addObject("newsClass", null);// 根据大分类 查询小分类 列表
			} else {
				List<ConfValue> plist = confService.queryByType(
						GlobalConstant.TYPE_NEWS, news.getCv().getPid());// 新闻分类
				mv.addObject("newsClass", plist);// 根据大分类 查询小分类 列表
				ConfValue cv = confService.find(news.getColumnId()); // 获取分类的父id,查询同级分类
				mv.addObject("typeName", cv.getValue0());// 分类的名称 用于 页面导航路径展示
			}
			mv.addObject("news", news);
			// 增加一次查看
			if (news != null) {
				news.setReadTimes(news.getReadTimes() == null ? 1 : news
						.getReadTimes() + 1);
				newsService.update(news);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "新闻详细内容页异常", e);
		}
		return mv;
	}

	/**
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/myNews")
	public ModelAndView myNews(News news) {
		ModelAndView mv = new ModelAndView("/my/myNewsInfo.jsp");
		try {
			Member mbo = (Member) session.getAttribute("user");
			if (mbo == null) {
				mv.setViewName("404.jsp");
			} else {
				news.setUserid(mbo.getId());
				// 设置分页信息
				if (news.getiDisplayLength() == 0)
					news.setiDisplayLength(10); // 默认长度5
				if (news.getPageNo() <= 0)
					news.setPageNo(1); // 默认页数1
				news.setiDisplayStart((news.getPageNo() - 1)
						* news.getiDisplayLength()); // 显示起始索引
				mv.addObject("dataList", newsService.query(news));
				mv.addObject("pageNo", news.getPageNo());
				mv.addObject("size", news.getiDisplayLength());
				mv.addObject("total", newsService.count(news));
				mv.addObject("news", news);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}

	/**
	 * 删除新闻
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/delNews")
	public ModelAndView delNews(News news, Long id) {
		ModelAndView mv = new ModelAndView("redirect:/news/myNews");
		try {
			Member mbo = (Member) session.getAttribute("user");
			if (mbo == null) {
				mv.setViewName("404.jsp");
			} else {
				newsService.delete(id);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}

	/**
	 * 初始化新闻
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/initNews")
	public ModelAndView initNews(News news, Long id) {
		ModelAndView mv = new ModelAndView("/my/newsDetail.jsp");
		mv.addObject("column",
				this.confService.queryByType(GlobalConstant.TYPE_NEWS, 9667L));
		Member mbo = (Member) session.getAttribute("user");
		if (mbo == null) {
			mv.setViewName("404.jsp");
		} else {
			news = newsService.find(id);
			mv.addObject("data", news);
		}
		return mv;
	}

	/**
	 * 保存新闻
	 * 
	 * @param news
	 * @return
	 */

	@RequestMapping("/saveNews")
	public ModelAndView saveNews(News news) {
		ModelAndView mv = new ModelAndView("redirect:/news/myNews");
		Member mbo = (Member) session.getAttribute("user");
		if (mbo == null) {
			mv.setViewName("404.jsp");
		} else {
			news.setUserid(mbo.getId());
			news.setCreateTime(DateUtil.getCurrentDate2());
			newsService.saveOrUpdate(news);
		}
		return mv;
	}
	
	/**
	 * 值班
	 * @param news
	 * @return
	 */
	@RequestMapping("/onDuty")
	public ModelAndView onDuty(News news) {
		ModelAndView mv = new ModelAndView("/news/onDuty.jsp");
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		
		return mv;
	}
	
	/**
	 * 论坛
	 * @param news
	 * @return
	 */
	@RequestMapping("/forum")
	public ModelAndView forums(News news) {
		ModelAndView mv = new ModelAndView("/forum/forumList.jsp");
		
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		return mv;
	}
	
	/**
	 * 特警 特警要闻：
	 * 查询全部更新的新闻
	 * 查询新闻列表
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView querys(News news) {
		ModelAndView mv = new ModelAndView("/news/newsList.jsp");
		try {
			// 设置分页信息
			if (news.getiDisplayLength() == 0)
				news.setiDisplayLength(12); // 默认长度24
			if (news.getPageNo() <= 0)
				news.setPageNo(1); // 默认页数1
			news.setiDisplayStart((news.getPageNo() - 1)
					* news.getiDisplayLength()); // 显示起始索引
			news.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			if (news.getKeyword() != null && !news.getKeyword().trim().isEmpty()) {
				mv.addObject("dataList",newsService.queryByLike(news, news.getKeyword()));
				mv.addObject("total", newsService.queryByLikeCount(news, news.getKeyword()));
			} else {
				mv.addObject("dataList", newsService.query(news));
				mv.addObject("total", newsService.count(news));
			}
			mv.addObject("pageNo", news.getPageNo());
			mv.addObject("size", news.getiDisplayLength());
			mv.addObject("typeName", "特警要闻");// 分类的名称 用于 页面导航路径展示
			mv.addObject("news", news);
			mv.addObject("key", news.getKeyword()); 
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}
	
	/**
	 * 特警首页初始化
	 * @param notice
	 * @return
	 */
	@RequestMapping("/inds")
	@ResponseBody
	public Map<String, Object> queryis(News notice) {
		Map<String, Object> resp = new HashMap<>();
		try {
			// 设置分页信息
			if (notice.getiDisplayLength() == 0)
				notice.setiDisplayLength(9); // 默认长度9
			if (notice.getPageNo() <= 0)
				notice.setPageNo(1); // 默认页数1
			notice.setiDisplayStart((notice.getPageNo() - 1)
					* notice.getiDisplayLength()); // 显示起始索引
			notice.setStatus("1");// 状态 0：无效，1：有效
			// 当关键字不为null并且不为空时，通过关键字全分类模糊查询
			// 反之，查询当前分页状态下的信息
			resp.put("dataList", newsService.query(notice));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询通知通报异常", e);
		}
		return resp;
	}
	
	
}
